package com.baichang.taoshunda.dao.impl;


import com.baichang.framework.dao.IDBCallBack;
import com.baichang.framework.dao.impl.BaseDaoImpl;
import com.baichang.framework.excelption.DBException;
import com.baichang.framework.model.Page;
import com.baichang.taoshunda.dao.CityDao;
import com.baichang.taoshunda.entity.City;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Repository
public class CityDaoImpl extends BaseDaoImpl<City> implements CityDao {


    /**
     * 接口获取热门城市
     *
     * @return
     */
    @Override
    public List<Map<String, Object>> getHotCity() {
        String sql = "SELECT * FROM t_city WHERE is_hot=1 and status=1";
        sql += " ORDER BY `sorting` asc";
        return queryForBeanList(sql);
    }

    /**
     * 接口 获取城市列表
     * 市一级
     *
     * @param parentId 父级编号  （预留，现在用不到）
     * @param key      关键字
     * @return
     */
    @Override
    public List<Map<String, Object>> getCityList(int parentId, String key) {

        String sql = "SELECT * FROM t_city WHERE dept_path=3 AND `status`=1";
        if (!key.equals("") || !key.isEmpty()) {
            sql = "SELECT * FROM t_city WHERE `name` LIKE '%" + key + "%' AND `status`=1";
            return queryForMapList(sql);
        }
        if (parentId > 1 && !key.isEmpty()) {
            sql = "SELECT * FROM t_city WHERE `parent_id` = ? and `name` LIKE '%" + key + "%' AND `status`=1";
            return queryForMapList(sql, parentId);
        }
        if (parentId > 1) {
            sql = "SELECT * FROM t_city WHERE `parent_id` = ? AND `status`=1";
            return queryForMapList(sql, parentId);
        }
        sql += " ORDER BY `sorting` DESC";
        return queryForMapList(sql);
    }

    /**
     * 根据上级编号获取城市信息
     *
     * @param parentId 上级编号
     * @return
     */
    @Override
    public List<Map<String, Object>> getDeliveryCityByParentId(int parentId) {
        String sql = " SELECT * FROM {table} WHERE status=1 AND parent_id=? ORDER BY sorting DESC ";
        return queryForMapList(sql, parentId);
    }

    /**
     * 热门城市列表
     */
    @Override
    public Page<Map<String, Object>> queryHotCity(int nowPage, int pageSize) throws DBException {
        StringBuffer stringBuffer = new StringBuffer();
        List<Object> params = new ArrayList<Object>();
        stringBuffer.append("SELECT * FROM t_city WHERE is_hot=1 and status=1 order by sorting asc");
        return pageForMapList(stringBuffer.toString(), nowPage, pageSize, params.toArray());
    }

    /**
     * 修改热门城市排名
     */
    @Override
    public int update(int sorting, int id) throws DBException {
        return update("update t_city a set a.sorting=" + sorting + " where id=" + id + "");
    }

    /**
     * 查询该城市下面有没有子集
     *
     * @param id
     */
    @Override
    public int findCategoryBySuperId(int id) throws DBException {
        String sql = "select * from t_city a where a.parent_id=" + id + " and a.status=1";
        return queryForInt(sql);
    }

    /**
     * 删除一个城市
     */
    @Override
    public int deleteCity(int id) throws DBException {
        String sql = "update t_city a set a.status=0 where id=" + id + "";
        return update(sql);
    }

    /**
     * 查询是否有同名的城市
     */
    @Override
    public int querySameCity(int id, String cityName) throws DBException {
        String sql = "select * from t_city a where name=\'" + cityName + "\' and id not in (" + id + ")";
        return queryForInt(sql);
    }

    /**
     * 修改城市名称
     */
    @Override
    public boolean updateCityName(int id, String cityName) throws DBException {
        String sql = "update t_city a set a.name=\'" + cityName + "\' where id=" + id + "";
        return update(sql) > 0;
    }

    /**
     * 后台查询全部城市
     */
    @Override
    public Page<City> queryAllCity(int nowPage, int pageSize, int parentId, int isHot, String name) throws DBException {
        List<Object> obj = new ArrayList<>();
        String sql = "select * from t_city where status in (1) and parent_id = ? ";
        obj.add(parentId);

        if (StringUtils.isNotBlank(name)) {
            sql += " and name like ?";
            obj.add("%" + name + "%");
        }
        if (isHot >= 1) {
            sql += " and is_hot = ?";
            obj.add(isHot);
        }
        sql += " ORDER BY sorting DESC,id DESC";
        return pageForBeanList(sql, nowPage, pageSize, obj.toArray());
    }


    /**
     * 查询城市名称
     */
    @Override
    public List<City> queryName(String name) {
        String sql = " SELECT * FROM t_city WHERE name LIKE ? ";
        return queryForBeanList(sql, name);
    }

    /**
     * 修改城市信息
     */
    @Override
    public int update(final Map<String, Object> map, final int id) throws DBException {
        return callBack(new IDBCallBack<Integer>() {
            @Override
            public Integer callBack() {
                return update(" id=? ", map, id);
            }
        });
    }

    /**
     * 查询热门城市 的个数
     */
    @Override
    public int hotCityCount() throws DBException {
        String sql = "select * from t_city a where is_hot=1 and status=1 and dept_path=3";
        List<City> list = queryForBeanList(sql);
        return queryForBeanList(sql).size();
    }

    /**
     * 获取城市信息
     *
     * @param deptPath
     * @return
     */
    @Override
    public List<Map<String, Object>> queryByCityId(Integer cityId, Integer deptPath) {
        if (cityId == 0) {
            String sql = "SELECT * FROM t_city WHERE dept_path=? AND status IN (1)";
            return queryForMapList(sql, deptPath);
        } else {
            String sql = "SELECT * FROM t_city WHERE dept_path=? AND status IN (1) AND parent_id=?";
            return queryForMapList(sql, deptPath, cityId);
        }
    }

    /**
     * 判断该parentId下面是否有重名的城市名   querySameName
     */
    @Override
    public List<City> querySameName(String name, Long parentId, int id) throws DBException {
        String sql = "";
        if (id == 0) { //是添加
            sql = "select * from t_city a where a.parent_id=" + parentId + " and status=1 and name=\'" + name + "\'";
        } else { //是修改
            sql = "select * from t_city a where a.parent_id=" + parentId + " and status=1 and name=\'" + name + "\' and id!=" + id + "";
        }

        return queryForBeanList(sql);
    }

    /**
     * 根据省id获取市
     *
     * @param id
     * @return
     */
    @Override
    public List<Map<String, Object>> queryCityIdInfo(Integer id) {
        String sql = "SELECT * FROM t_city WHERE parent_id=? AND status IN (1)";
        return queryForMapList(sql, id);
    }

    @Override
    public int getCityIdByCityName(String cityName) {
        String sql = "SELECT IFNULL((SELECT id AS cityId FROM t_city WHERE `name` LIKE '%" + cityName + "%' AND `status`=1 AND dept_path=3),140) AS cityId";
        return queryForInt(sql);
    }

    /**
     * 根据城市编号获取城市信息
     *
     * @param cityId 城市编号
     * @return
     */
    @Override
    public City getCityById(Integer cityId) {
        String sql = " SELECT * FROM {table} WHERE id=? AND status=1 ";
        return queryForBean(sql, cityId);
    }

    /**
     * 查询省级名字
     *
     * @param parentId
     * @return
     */
    @Override
    public Map<String, Object> getCityInfo(Long parentId) {
        String sql = " SELECT * FROM {table} WHERE id=? AND status=1 ";
        return queryForMap(sql, parentId);
    }

    @Override
    public int getCityIdByName(String cityName, Integer level) {
        String sql = "SELECT id FROM `t_city` WHERE `dept_path` = ? AND `name` LIKE '%" + cityName + "%' AND `status` = '1' LIMIT 0, 1000";
        return queryForInt(sql, level);
    }
}